/**
 * 
 */
package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.alee.laf.panel.WebPanel;
import com.alee.laf.scroll.WebScrollPane;

import controller.Controls;

import model.Recipe;
import model.RecipeIngredient;
import model.RecipeStep;

/**
 * Panel provides a readable display of a chosen recipe.
 * 
 * @author
 */
public class RecipeDisplayPanel extends JPanel {
    /**
     * Serial Version ID.
     */
    private static final long serialVersionUID = 6042397695189991408L;
    private static final Dimension PIC_SIZE = new Dimension(150, 150);
    private static final Dimension PIC_HOLDER = new Dimension(160, 160);
    private static final Dimension DIRECTION_MAX = new Dimension(450, 300);
    private static final Dimension RECIPE_DISPLAY_PREFER = new Dimension(475, 400);
    private static final Dimension VIEWPORT_MAX = new Dimension(475, 550);
    /**
     * 
     */
    private static final Dimension RECIPE_DISPLAY_MAX = new Dimension(500, 600);
    /**
     * 
     */
    private static final String SERVES = " Serves: ";
    /**
     * 
     */
    private static final String PREP = "  Prep Time: ";
    /**
     * 
     */
    private static final String COOK = "  Cook Time: ";
    /**
     * 
     */
    private static final String LEVEL = "  Level: ";
    
    //private final JButton my_favoriteB;
    
    private final Recipe my_recipe; 
    
    private final Controls my_controller;
    
    
    public RecipeDisplayPanel(Controls the_controller, Recipe the_recipe) {
        my_recipe = the_recipe;
        my_controller = the_controller;
        //this.setPreferredSize(RECIPE_DISPLAY_MAX);
        //this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setBackground(Color.WHITE);
        this.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setupDisplay();
    }
    public void setupDisplay() {
        JPanel masterPanel = new JPanel();
        masterPanel.setOpaque(false);
        masterPanel.setLayout(new BoxLayout(masterPanel, BoxLayout.PAGE_AXIS));
        masterPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0 , 0));
        //create Buttons
        JButton favoriteButton = makeFavoriteButton();
        JButton editButton = makeEditButton();
        
        //Create Labels
        JLabel titleLabel = new JLabel(my_recipe.getTitle());
        titleLabel.setBorder(BorderFactory.createEmptyBorder(15, 0, 0, 0));
        titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
        JLabel authorLabel = new JLabel("By " + my_recipe.getAuthor());
        authorLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 10, 0));
        JLabel ingredLabel = new JLabel("Ingredients");
        ingredLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 0));
        
        //Serves, prep, cook, and level setup
        JPanel timePanel = buildTimePanel();
        
        //Ingredients grid
        JPanel ingredientGrid = buildIngredientPanel();

        //Directions
        JPanel directionPanel = buildDirectionPanel();
        
        //Top panel with title and buttons
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.X_AXIS));
        //titlePanel.add(titleLabel);
        titlePanel.add(favoriteButton);
        titlePanel.add(editButton);
        
        //Ingredients and Picture
        JPanel ingredPanel = new JPanel();
        ingredPanel.setLayout(new BoxLayout(ingredPanel, BoxLayout.X_AXIS));
        ingredientGrid.setMaximumSize(ingredientGrid.getPreferredSize());
        ingredPanel.add(ingredientGrid);
        if (my_recipe.getPicture() != null && !my_recipe.getPicture().isEmpty()) {
            BackgroundPanel pic = new BackgroundPanel(BackgroundPanel.createBackgroundImage(this, my_recipe.getPicture()), 0);
            pic.setMaximumSize(PIC_SIZE);
            pic.setPreferredSize(PIC_SIZE);
            pic.setBorder(BorderFactory.createEtchedBorder());
            ingredPanel.add(pic);
        }
        //Alignment
        titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        titlePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        authorLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        timePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        ingredLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        ingredPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        directionPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        //Opacity
        titlePanel.setOpaque(false);
        titleLabel.setOpaque(false);
        authorLabel.setOpaque(false);
        timePanel.setOpaque(false);
        ingredLabel.setOpaque(false);
        ingredPanel.setOpaque(false);
        //Final Construction
        masterPanel.add(titlePanel);
        masterPanel.add(titleLabel);
        masterPanel.add(authorLabel);
        masterPanel.add(timePanel);
        masterPanel.add(ingredLabel);
        masterPanel.add(ingredPanel);
        masterPanel.add(directionPanel);
        //masterPanel.setPreferredSize(RECIPE_DISPLAY_PREFER);
        //final JScrollPane masterScroll = new JScrollPane(masterPanel);
        final WebScrollPane masterScroll = new WebScrollPane(masterPanel);
        //Sets Scroll bar to top.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() { 
                masterScroll.getVerticalScrollBar().setValue(0);
            }
         });

        masterScroll.getViewport().setOpaque(false);
        masterScroll.setOpaque(false);
        masterScroll.getWebUI().setDrawBorder(false);
        //masterScroll.getViewport().setOpaque(false);
        //masterScroll.setOpaque(false);
        masterScroll.getViewport().setPreferredSize(RECIPE_DISPLAY_PREFER);
        //masterScroll.getViewport().setMaximumSize(VIEWPORT_MAX);
        //JPanel nest = new JPanel();
        //nest.setLayout(new BoxLayout(nest, BoxLayout.X_AXIS));
        //nest.add(masterScroll);
        //nest.setOpaque(false);
        this.add(masterScroll);
    }
    public JPanel buildDirectionPanel() {
        JPanel result = new JPanel(new BorderLayout());
        result.setOpaque(false);
        JPanel directionPanel = new JPanel();
        directionPanel.setLayout(new BoxLayout(directionPanel, BoxLayout.Y_AXIS));
        
        JLabel directionLabel = new JLabel("Directions");
        directionLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
        List<RecipeStep> stepList = my_recipe.getSteps();
        if (stepList != null) {
        for (int i = 0; i < stepList.size(); i++) {
            RecipeStep step = stepList.get(i);
            JTextArea stepField = new JTextArea(1 + step.getNumber() + ". " + step.getDirections() + "\n");
            stepField.setLineWrap(true);
            stepField.setWrapStyleWord(true);
            //stepField.setColumns(10);
            directionPanel.add(stepField);
        }
        }
        result.add(directionLabel, BorderLayout.NORTH);
        result.add(directionPanel, BorderLayout.CENTER);
        //result.setPreferredSize(DIRECTION_MAX);
        //result.setMaximumSize(DIRECTION_MAX);
        return result;
    }
    public JPanel buildIngredientPanel() {
        JPanel ingredientGrid = new JPanel();
        ingredientGrid.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));
        ingredientGrid.setOpaque(false);
        List<RecipeIngredient> ingredientList = my_recipe.getIngredients();
        if (ingredientList != null) {
        GridLayout grid = new GridLayout(ingredientList.size(), 1);
        grid.setHgap(2);
        grid.setVgap(2);
        ingredientGrid.setLayout(grid);
        for (int i = 0; i < ingredientList.size(); i++) {
            JLabel ingredientLabel = new JLabel(ingredientList.get(i).toString());
            ingredientLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
            ingredientLabel.setOpaque(false);
            ingredientGrid.add(ingredientLabel);
        }
        }
        return ingredientGrid;
    }
    public JPanel buildTimePanel() {
        JPanel timePanel = new JPanel();
        timePanel.setLayout(new BoxLayout(timePanel, BoxLayout.X_AXIS));
        JLabel serves = new JLabel(SERVES + my_recipe.getServes() + " ");
        JLabel prep = new JLabel(PREP + my_recipe.getPrepTime() + " ");
        JLabel cook = new JLabel(COOK + my_recipe.getCookTime() + " ");
        JLabel level = new JLabel(LEVEL + my_recipe.getDifficulty() + " ");
        //opacity
        timePanel.setOpaque(false);
        serves.setOpaque(false);
        level.setOpaque(false);
        prep.setOpaque(false);
        cook.setOpaque(false);
        //borders
        serves.setBorder(BorderFactory.createDashedBorder(null));
        prep.setBorder(BorderFactory.createDashedBorder(null));
        cook.setBorder(BorderFactory.createDashedBorder(null));
        level.setBorder(BorderFactory.createDashedBorder(null));
        
        timePanel.add(serves);
        timePanel.add(prep);
        timePanel.add(cook);
        timePanel.add(level);
        return timePanel;
    }
    public JButton makeFavoriteButton() {
        final JButton favoriteB = new JButton();
        setFavoriteButton(my_controller.isFavorite(my_recipe), favoriteB);
        favoriteB.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), 
                                                                BorderFactory.createEmptyBorder(0, 10, 0, 10)));
        favoriteB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent the_event) {
                //favorite action here
                if (favoriteB.getForeground() == Color.WHITE) {
                    setFavoriteButton(true, favoriteB);
                    my_controller.addFavorite(my_recipe);
                } else {
                    favoriteB.setBackground(new Color(128, 128, 128));
                    setFavoriteButton(false, favoriteB);
                    my_controller.removeFavorite(my_recipe);
                }
            }
        });
        return favoriteB;
    }
    private void setFavoriteButton(boolean the_value, JButton the_favoriteB) {
        if (the_value) {
            the_favoriteB.setForeground(Color.BLACK);
            the_favoriteB.setBackground(Color.YELLOW);
            the_favoriteB.setText("Favorited");
        } else {
            the_favoriteB.setBackground(new Color(128, 128, 128));
            the_favoriteB.setForeground(Color.WHITE);
            the_favoriteB.setText("Favorite");
        }
    }
    public JButton makeEditButton() {
        JButton editB = new JButton("Edit");
        editB.setBackground(new Color(128, 128, 128));
        editB.setForeground(Color.WHITE);
        editB.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), 
                                                                BorderFactory.createEmptyBorder(0, 10, 0, 10)));
        editB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent the_event) {
                my_controller.goEditRecipe(my_recipe);
            }
        });
        return editB;
    }
    
    
}
